<?php 
namespace lib;
trait DBTraits{
    //辅助方法
    /**
     * 通用单行查询方法
     * @param string $table 表名（不含前缀）
     * @param mixed $value 查询值
     * @param string $field 查询字段名
     * @param string $select 要查询的列
     * @return array|null
     */
    public function getRow($table, $value, $field = 'id', $select = '*') {
        $prefix = dbconfig['sqlname'] ?? 'pre_';
        $table = $this->formatTableName($table, $prefix);
        
        $sql = "SELECT {$select} FROM `{$table}` WHERE `{$field}` = :value LIMIT 1";
        $params = ['value' => $value];
        
        $result = safeSqlQuery($sql, $params, 'select', ['single_row' => true]);
        return $result['result'] ?? null;
    }
    /**
     * 格式化表名（处理特殊表名）
     * @param string $table
     * @param string $prefix
     * @return string
     */
    private function formatTableName($table, $prefix) {
        // 如果表名已经包含前缀，直接返回
        if (strpos($table, $prefix) === 0) {
            return $table;
        }
        return $prefix . $table;
    }
    
    public function userrow($user, $name = 'user', $select = '*') {
        return $this->getRow('user', $user, $name, $select);
    }
    
    public function adminrow($user) {
        return $this->getRow('admin', $user, 'user');
    }
    
    public function orderrow($value, $name = 'trade_no', $select = '*') {
        return $this->getRow('order', $value, $name, $select);
    }
    
    public function qrlistrow($value, $name = 'id', $select = '*') {
        return $this->getRow('qrlist', $value, $name, $select);
    }
    
    public function wechat_trumpetrow($value, $name = 'id', $select = '*') {
        return $this->getRow('wechat_trumpet', $value, $name, $select);
    }
    public function packagerow($value, $name = 'id', $select = '*') {
        return $this->getRow('package', $value, $name, $select);
    }
    public function noticerow($value, $name = 'id', $select = '*') {
        return $this->getRow('notice', $value, $name, $select);
    }
    public function downrow($value, $name = 'id', $select = '*') {
        return $this->getRow('down', $value, $name, $select);
    }
    public function workrow($value, $name = 'id', $select = '*') {
        return $this->getRow('work', $value, $name, $select);
    }
    public function emailrow($value, $name = 'id', $select = '*') {
        return $this->getRow('email', $value, $name, $select);
    }
    public function wxyunurlrow($value, $name = 'id', $select = '*') {
        return $this->getRow('wxyunurl', $value, $name, $select);
    }
    public function qrlistnum($pid){
       return $this->select('qrlist',"count(*) as count" , ['pid'=>$pid] , ['single_row'=>true])['result']['count'];
    }
}

?>